import 'whatwg-fetch'
import 'es6-promise'

class FetchPersonal {
  constructor() {
    this.url='';
    this.method='GET';
    this.headers={'Accept': 'application/json, text/plain, */*'};
    // this.headers={'Accept': 'text/html,application/xhtml+xml,application/xml'};
    this.body='';
    this.credentials='include';
  }
  setHeaders(headers){
    this.headers=JSON.parse(JSON.stringify(headers));
    return this;
  }
  setCredentials(credentials){
    this.credentials=credentials;
    return this;
  }
  async fetch(url,fetchObj){
    let returnFetchGet=fetch(url,fetchObj);
    return returnFetchGet;
  }
}

export default class MyFetch extends FetchPersonal{
  constructor() {
    super();
  }
  test(){
    console.log(this.headers)
  }
  async fetchGet(url){
    let fetchObj={
      method:this.method,
      credentials:this.credentials,
      headers:this.headers
    }
    let result,status,dataJson;
    try{
       result=await super.fetch(url,fetchObj);
       status=await result.status;
       dataJson=await result.json();
    }catch(e){
      console.log(e)
    }
    console.log(status)
    console.log(result)
    console.log(dataJson)
    if(status=='200'){
      return dataJson;
    }else {
      return {message:'网络出错了！'}
    }
  }
}
// let myfetch=new MyFetch();
// myfetch.cathTest();
